package blazingbubble.gui.panels;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import blazingbubble.gui.INavigationButton;
import blazingbubble.gui.ISwitchable;
import blazingbubble.gui.PanelRegistry;
import blazingbubble.gui.Resources;
import blazingbubble.gui.elements.MenuButton;
import blazingbubble.gui.elements.MenuLabel;
import blazingbubble.gui.elements.MenuTextField;

public class SettingsPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 7800597414644965764L;
	
	public static final String PANEL_NAME = "Settings";
	
	private static final Image BACKGROUND_IMAGE = Toolkit.getDefaultToolkit().createImage(Resources.BACKGROUND_NOTITLE_IMAGEPATH);
	private static final String PLAYERNAMELABEL_TEXT = "Player Name:";
	
	private MenuButton backButton = new MenuButton(Resources.BACKBUTTON_IMAGEPATH);
	private MenuButton okayButton = new MenuButton(Resources.OKAYBUTTON_IMAGEPATH);
	
	private MenuLabel playerNameLabel = new MenuLabel(PLAYERNAMELABEL_TEXT, Font.BOLD, 12);
	private MenuTextField playerNameField = new MenuTextField(Resources.TEXTFIELD_WIDE_BORDERS_IMAGEPATH, 0, Font.PLAIN, 12);
	
	private ISwitchable parentFrame = null;
	
	private String playerName = System.getProperty("user.name");
	
	public SettingsPanel(ISwitchable parentFrame){
		this.parentFrame = parentFrame;
		PanelRegistry.getInstance().registerPanel(PANEL_NAME, this);
		setUp();
	}
	
	private void setUp(){
		this.setLayout(null);
		this.add(playerNameLabel);
		playerNameLabel.setBounds(5, 5, 335, 25);
		
		playerNameField.setText(playerName);
		this.add(playerNameField);
		playerNameField.setBounds(5, 30, 335, 35);
		
		backButton.setDestinationPanel(PanelRegistry.getInstance().lookUpPanel(MainMenuPanel.PANEL_NAME));
		backButton.addActionListener(this);
		this.add(backButton);
		backButton.setBounds(5, 360, 165, 55);
		
		okayButton.setDestinationPanel(PanelRegistry.getInstance().lookUpPanel(MainMenuPanel.PANEL_NAME));
		okayButton.addActionListener(this);
		this.add(okayButton);
		okayButton.setBounds(175, 360, 165, 55);
	}
	
	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getSource().equals(okayButton)){
			playerName = playerNameField.getText();
		}
		parentFrame.switchPanel(((INavigationButton)event.getSource()).getDestinationPanel());	
	}
	
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		g.drawImage(BACKGROUND_IMAGE, 0, 0, getWidth(), getHeight(), this);
	}
	
	public String getPlayerName(){
		return playerName;
	}
}
